<?php
/**
 * (model) CN-海外仓-中转发货箱号明细（批次）（国内发海外仓）
 */
namespace App\Models\OverSeasIn;

use Illuminate\Database\Eloquent\Model;
class DeliveryBoxDetail extends Model
{
    protected $table = 'os_in_delivery_box_detail';

    protected $guarded = [];

    const INBOUND_IN = 1;  //待入库
    const INBOUND_YES = 2; //已入库
    const INBOUND_PAY = 3; //已赔付
    public static $_status = [
        self::INBOUND_IN  => '待入库',
        self::INBOUND_YES => '已入库',
        self::INBOUND_PAY => '已赔付',
    ];

    public function box(){
        return $this->belongsTo(DeliveryBox::class,'delivery_box_id','id');
    }

    /**
     * 回写boxDetail数据
     * @param $deliveryBoxId
     * @param $total
     * @param $storageTotal
     * @param $thisStorageTotal
     * @param $dealAt
     * @param int $status
     */
    public static function _upBoxDetail($deliveryBoxId,$total,$storageTotal,$thisStorageTotal,$dealAt,$status = 2){
        for ($i = 1;$i <= $thisStorageTotal;$i++){
            $pIndex = $i + $storageTotal;
            if($pIndex > $total) break;
            $boxDetail = DeliveryBoxDetail::query()
                ->where('delivery_box_id',$deliveryBoxId)
                ->where('status',1)
                ->where('p_index',$pIndex)
                ->first();
            if($boxDetail){
                $boxDetail->status = $status; //入库状态 (1:待入库 2:已入库 3:已赔付)
                $boxDetail->deal_at = $dealAt; //入库时间
                $boxDetail->save();
            }
        }
    }

}
